package com.lotter.api.service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lotter.common.enumobject.BindCardOperateEnum;
import com.lotter.common.exception.LotterCusException;
import com.lotter.common.util.MD5Util;
import com.lotter.dao.BankCardMapper;
import com.lotter.model.bankcard.BankCard;


@Service
public class BankCardService {
	
	@Autowired
	private BankCardMapper bankCardMapper;
	
	
	/**
	 * 判断是否已经绑卡
	 * @param uid
	 * @return
	 * @throws LotterCusException
	 */
	@Transactional(rollbackFor=Exception.class)
	public Boolean findBankCardByUid(Long uid) throws LotterCusException{
		Map<String,Object> result = new HashMap<String, Object>();
		result.put("uid", uid);
		result.put("status", BindCardOperateEnum.BIND.getCode());
		int number = 0 ;
		number = bankCardMapper.findBankCardByMap(result);
		if(number > 0 ){
			return true;
		}
		return false;
	}
	
	
	public BankCard findBank(Long uid) throws LotterCusException{
		BankCard bankCard = null;
		Map<String,Object> result = new HashMap<String, Object>();
		result.put("uid", uid);
		result.put("status", BindCardOperateEnum.BIND.getCode());
		bankCard = bankCardMapper.findBankDTO(result);
		return bankCard;
	}
	
	//新增
	@Transactional(rollbackFor=Exception.class)
	public void insert(BankCard bankCard) throws LotterCusException{
		bankCardMapper.insert(bankCard);
	}

	//新增新卡
	@Transactional(rollbackFor=Exception.class)
	public void update(BankCard bankCard,String realname,String bankName,String bankcard) throws LotterCusException{
		bankCard.setStatus(BindCardOperateEnum.UNBIND.getCode());
		bankCardMapper.updateByPrimaryKeySelective(bankCard);
		bankCard.setRealname(realname);
		bankCard.setBankName(bankName);
		bankCard.setBankcard(bankcard);
		bankCard.setStatus(BindCardOperateEnum.BIND.getCode());
		bankCard.setCreatedate(new Date());
		bankCardMapper.insert(bankCard);
	}
	
	//更新支付密码
	@Transactional(rollbackFor=Exception.class)
	public void updateParpwd(Long id,String newPaypwd) throws LotterCusException{
		BankCard bankCard = new BankCard();
		bankCard.setId(id);
		bankCard.setPaypwd(MD5Util.md5Hex(newPaypwd));
		bankCardMapper.updateByPrimaryKeySelective(bankCard);
	}
	

}
